package com.chara.websocketdemo.config;

import com.chara.websocketdemo.handler.MyTextWebSocketHandler;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * @Classname WebSocketConfig
 * @Description  WebSocket配置类
 * @Date 2025/8/20 16:35
 * @Author Chara
 */
@Configuration
@EnableWebSocket
@RequiredArgsConstructor
public class WebSocketConfig implements WebSocketConfigurer {

    @Resource
    private MyTextWebSocketHandler myTextWebSocketHandler;


    /**
     * 注册WebSocket处理器
     */
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(myTextWebSocketHandler, "/ws")
                .setAllowedOriginPatterns("*");//允许所有来源，仅适用于开发环境，生产环境需做更改
//                .withSockJS(); // 可选项，支持SockJS降级
    }
}
